package com.atguigu.mybatisplus.controller;

import com.atguigu.mybatisplus.common.result.Result;
import com.atguigu.mybatisplus.model.entity.Role;
import com.atguigu.mybatisplus.model.form.RoleForm;
import com.atguigu.mybatisplus.model.query.RolePageQuery;
import com.atguigu.mybatisplus.model.vo.DataRolePageVO;
import com.atguigu.mybatisplus.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色接口")
@RestController
@RequestMapping("/api/v1/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色列表
     * @param rolePageQuery
     * @return
     */
    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    public Result<DataRolePageVO> pageRole(@Valid @ParameterObject RolePageQuery rolePageQuery) {
        DataRolePageVO dataRolePageVO = roleService.getRolePage(rolePageQuery);
        return Result.success(dataRolePageVO);
    }
    /**
     * 新增角色
     * @param roleForm
     * @return
     */
    @PostMapping
    @Operation(summary = "新增角色")
    public Result<String> addRole(@RequestBody @Valid RoleForm roleForm) {
        roleService.saveRole(roleForm);
        return Result.success("新增成功");
    }
    /**
     * 根据角色id查询角色表单信息
     * @param roleId
     * @return
     */
    @Operation(summary = "角色表单数据")
    @GetMapping("/{roleId}/form")
    public Result<RoleForm> getRoleForm(@PathVariable Long roleId){
        Role sysRole = roleService.getById(roleId);
        RoleForm sysRoleForm = new RoleForm();
        BeanUtils.copyProperties(sysRole,sysRoleForm);
        return Result.success(sysRoleForm);
    }
    /**
     * 更新角色
     * @param sysRoleForm
     * @return
     */
    @Operation(summary = "更新角色")
    @PutMapping("/{roleId}")
    public Result<String> updateRole(@RequestBody @Valid RoleForm roleForm){
        roleService.updateRole(roleForm);
        return Result.success("修改成功");
    }
    /**
     * 删除角色
     * @param ids
     * @return
     */
    @Operation(summary = "删除角色")
    @DeleteMapping("/{ids}")
    public Result<String> deleteRole(@PathVariable List<Long> ids){
        roleService.removeByIds(ids);
        return Result.success("删除成功");
    }
    /**
     * 获取角色的菜单ID列表
     * @param roleId
     * @return
     */
    @Operation(summary = "获取角色的菜单ID集合")
    @GetMapping("/{roleId}/menuIds")
    public Result<List<Integer>> getMenuIds(@PathVariable Long roleId){
        List<Integer> menuIds = roleService.getMenuIds(roleId);
        return Result.success(menuIds);
    }
    /**
     * 更新角色状态
     * @param roleId
     * @param status
     * @return
     */
    @Operation(summary = "更新角色状态")
    @PutMapping("/{roleId}/status")
    public Result<String> updateStatus(@PathVariable Long roleId, @Valid Integer status){
        roleService.updateStatus(roleId,status);
        return Result.success("更新成功");
    }
}
